package com.lemon.smartsafety.view.list;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.AbsListView;
import android.widget.ListView;


/**
 * 作者：machenike on 2016/4/27 10:38
 * 邮箱：
 * <p>
 * 作用:
 */
public class ListRefreshView extends ListView implements AbsListView.OnScrollListener {

    private ScrollBottomListener scrollBottomListener;
    private boolean isBtm = true;

    private boolean isload=true;

    public void setIsload(boolean isload) {
        this.isload = isload;
    }

    public boolean isload() {
        return isload;
    }

    public ListRefreshView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setOnScrollListener(this);
    }

    public ListRefreshView(Context context, AttributeSet as) {
        super(context, as);
        // TODO Auto-generated constructor stub
        setOnScrollListener(this);

    }



    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        try {
            switch (scrollState) {
                // 当不滚动时
                case OnScrollListener.SCROLL_STATE_IDLE:
                    // 判断滚动到底部
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
                        //LogUtils.LOGE("判断滚动到底部");
                        if(isload) {
                            if (scrollBottomListener != null && isBtm) {
                                isBtm = false;
                                scrollBottomListener.scrollBottom();
                            }
                        }
                    }
                    //回滚
                    else if (view.getLastVisiblePosition() < (view.getCount() - 1)) {
                        //LogUtils.LOGE("当不滚动时,回滚");
                        isBtm = true;
                    }
                    // 判断滚动到顶部
                    else if (view.getFirstVisiblePosition() == 0) {
                        //LogUtils.LOGE("判断滚动到顶部");

                    }
                    break;
                // 滑动
                case SCROLL_STATE_TOUCH_SCROLL:
                    //回滚
                    if (view.getLastVisiblePosition() < (view.getCount() - 1)) {
                        //LogUtils.LOGE("滑动,回滚");
                        isBtm = true;
                    }
                    break;
                // 和惯性滑动
                case SCROLL_STATE_FLING:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //
    /*     View c = view.getChildAt(0);
        if (c == null) {
            return;
        }
        int scrolly = -c.getTop() + view.getPaddingTop() + view.getFirstVisiblePosition() * c.getHeight();
        LogUtils.LOGE("scrolly："+scrolly);
        LogUtils.LOGE("computeVerticalScrollRange："+computeVerticalScrollRange());*/
    }


    public void setScrollBottomListener(ScrollBottomListener scrollBottomListener) {
        this.scrollBottomListener = scrollBottomListener;
    }

    public interface ScrollBottomListener {
        public void scrollBottom();
    }
}

